package com.windoent.yugioh.ui.activity.setting;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.baidu.mobstat.StatService;
import com.windoent.yugioh.R;
import com.windoent.yugioh.base.BaseActivity;
import com.windoent.yugioh.bean.GeneralBean;
import com.windoent.yugioh.http.PhoneLive;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.BaseUtils;
import com.windoent.yugioh.utils.SimpleUtils;
import com.windoent.yugioh.utils.ToastUtils;
import com.windoent.yugioh.utils.XingUtils;
import com.windoent.yugioh.widget.ActivityTitle;
import com.windoent.yugioh.widget.PhoneCode;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;

import java.lang.ref.WeakReference;

import okhttp3.Call;

/**
 * author : robert
 * date   : 2021/5/12 17:07
 * desc   :修改邮箱验证手机号
 */
public class SettingChangeEmailVerifyPhoneActivity extends BaseActivity {
    private ActivityTitle mActivityTitle;
    private TextView mTvPhone;
    private TextView mTvSend;
    private PhoneCode mPhoneCode;
    private Button mBtnNext;
    private String phone;
    private String code;


    @Override
    public int getLayout() {
        return R.layout.activity_setting_change_email_verify_phone;
    }

    @Override
    public void initView() {
        super.initView();

        mActivityTitle = (ActivityTitle) findViewById(R.id.activity_title);
        immersiveStatusBar(mActivityTitle.getView());
        mTvPhone = (TextView) findViewById(R.id.tv_phone);
        mTvSend = (TextView) findViewById(R.id.tv_send);
        mPhoneCode = (PhoneCode) findViewById(R.id.phone_code);
        mBtnNext = (Button) findViewById(R.id.btn_next);
    }

    @Override
    public void initData() {
        super.initData();
        Intent intent = getIntent();
        phone = intent.getStringExtra("phone");
        mTvPhone.setText(XingUtils.xingPhone(phone));
        startTimer();
    }

    @Override
    public void initListener() {
        super.initListener();
        mActivityTitle.setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mTvSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getCellPhoneCode(phone);
            }
        });
        mBtnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (BaseMethod.isNull(code) || code.length() < 5) {
                    ToastUtils.showText(getString(R.string.string_input_phone_verify_code));
                    return;
                }
                verifyCellPhoneCode(phone, code);
            }
        });
        mPhoneCode.setOnInputListener(new PhoneCode.OnInputListener() {
            @Override
            public void onSucess(String code) {

            }

            @Override
            public void onInput(String phoneCode) {
                code = phoneCode;
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }

    //验证手机号
    private void verifyCellPhoneCode(String phone, String code) {
        showLoadingDialog("");
        PhoneLive.verifyCellPhoneCode(phone, code, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            GeneralBean json = gson.fromJson(response, GeneralBean.class);
                            ToastUtils.showText(json.getMsg());
                            startActivity(new Intent(mContext, SettingChangeEmailActivity.class));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.showText(getString(R.string.string_response_error));
                }
            }
        });
    }

    //发送验证码
    private void getCellPhoneCode(String phone) {
        showLoadingDialog("");
        PhoneLive.getCellPhoneCode(phone, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            startTimer();
                            GeneralBean generalBean = gson.fromJson(response, GeneralBean.class);
                            ToastUtils.showText(generalBean.getMsg());
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.showText(getString(R.string.string_response_error));
                }

            }
        });
    }

    //开始倒计时
    private void startTimer() {
        mTvSend.setEnabled(false);
        SimpleUtils.startTimer(new WeakReference<TextView>(mTvSend), getString(R.string.string_send_phone_code_number), 60, 1, new SimpleUtils.TimerListener() {
            @Override
            public void onFinished() {
                mTvSend.setEnabled(true);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onPageStart(this, "设置修改邮箱验证手机号界面");
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPageEnd(this, "设置修改邮箱验证手机号界面");
    }
}
